﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class wuc_CustomDatePicker : System.Web.UI.UserControl
{
    string dateFormat = "yyyy/MM/dd";

    public bool IsMandatory
    {
        get { return this.MainRequiredFieldValidator.Enabled; }
        set { }
    }

    public string Text
    {
        get { return this.MainTextBox.Text; }
        set { }
    }

    public bool OnOrAfterToday
    {
        get { return Convert.ToBoolean(this.OnOrAfterTodayHiddenField.Value); }
        set { }
    }

    public string ValidationGroup
    {
        get { return this.MainTextBox.ValidationGroup; }
        set { }
    }

    public bool Validate()
    {
        if (this.MainRequiredFieldValidator.Enabled)
        {
            Page.Validate("Main");
            return Page.IsValid;
        }
        else
        {
            return true;
        }
    }

    public string GetSelectedDate()
    {
        return this.MainTextBox.Text;
    }

    public void SetDateText(string date)
    {
        this.MainTextBox.Text = date;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.MainRequiredFieldValidator.Text = "Date must be on or after today";// Utilities.getLabel("ALERT_REQUIRED_FIELD", "LABEL");
            this.DateFormatValidator.ErrorMessage = "Required field"; // Utilities.getLabel("ALERT_REQUIRED_FIELD") & ": " & dateFormat;
            this.MinDateValidator.ErrorMessage = "Required field";// Utilities.getLabel("ALERT_DATE_AFTER_TODAY");
            this.MinDateValidator.Enabled = OnOrAfterToday;
            this.MinDateValidator.MinimumValue = DateTime.Today.ToString(dateFormat);
            this.MinDateValidator.MaximumValue = DateTime.MaxValue.ToString(dateFormat);
        }
    }

    public void CheckDateFormat(object source, ServerValidateEventArgs args)
    {
        DateTime dt = new DateTime();
        args.IsValid = DateTime.TryParseExact(args.Value, dateFormat, null, System.Globalization.DateTimeStyles.None, out dt);
    }
    
}